var app = getApp();
Page({
  data: {
    productId:0,
    customName:'',
    customPeople:'',
    customPhone:'',
    customDescrption:'',
    returnType:'',
    radioItems: [
      {name:'线索',checked: true},
      { name: '引荐'},
      { name: '直签' }
    ],
    valideCode: '', 
    time: "获取验证码",
    currentTime: 61,
    disabled: false,
    suffix: '',
  },
  valideCodeInput(e){
    this.setData({
      valideCode: e.detail.value
    })
  },
  sendCode(){
    let _this = this;
    if (!_this.data.disabled) {
      _this.getCode();
    }
  },
  onShareAppMessage: function () {

  },
  getCode(){
    let _this = this;

      app.request({
        url: 'api/wechat/sendCode',
        method: "GET",
        success: function (res, header) {
          // var index1 = header["Set-Cookie"].indexOf("JSESSIONID")
          // var index2 = header["Set-Cookie"].indexOf(";", index1)
          // var JSESSIONID = header["Set-Cookie"].substring(index1, index2)
          // wx.setStorageSync('cookie', wx.getStorageSync("cookie") + ";" + JSESSIONID)

          wx.showToast({
            title: '已发送',
          })
          _this.setData({
            disabled: true
          })
          let interval = null;
          let currentTime = _this.data.currentTime;

          interval = setInterval(function () {
            currentTime--;
            _this.setData({
              time: currentTime,
              suffix: '秒后可重新获取'
            })
            if (currentTime <= 0) {
              clearInterval(interval)
              _this.setData({
                time: '重新发送',
                suffix: '',
                currentTime: 61,
                disabled: false
              })
            }
          }, 1000)
        }
      })
  },
  customNameInput(e){
    this.setData({
      customName:e.detail.value
    })
  },

  customPeopleInput(e){
    this.setData({
      customPeople:e.detail.value
    })
  },
  customPhoneInput(e){
    this.setData({
      customPhone:e.detail.value
    })
  },
  customDescrptionInput(e){
    this.setData({
      customDescrption:e.detail.value
    })
  },
  radioChange(e) {
    const checked = e.detail.value
    const changed = {}
    for (let i = 0; i < this.data.radioItems.length; i++) {
      if (checked.indexOf(this.data.radioItems[i].name) !== -1) {
        changed['radioItems[' + i + '].checked'] = true
      } else {
        changed['radioItems[' + i + '].checked'] = false
      }
    }
    this.setData(changed)
    
  },
  onLoad: function (options) {
    this.setData({
      productId:options.id
    })
    this.loadDetail(options.id)
    app.request({
      url: 'api/wechat/getUserInfo',
      method: "GET",
      success: function (res) {
        if (res != null && res.data != null) {
          self.setData({
            username: res.data.info.userName
          })
        }
      }
    })
  },
  loadDetail: function (id) {
    let self = this
    app.request({
      url: 'api/wechat/getProductById',
      method: "GET",
      data: {
        id:id
      },
      success: function (res) {
        console.log(res)
        if(res.data.result){
          var xiansuo = res.data.product.xiansuo
          var yinqian = res.data.product.yinqian
          var zhiqian = res.data.product.zhiqian
          var radioItems = []
          if(xiansuo != null && xiansuo != '' && xiansuo != '0'){
            radioItems.push({name:'线索'})
            
          }
          if(yinqian != null && yinqian != '' && yinqian != '0'){
            radioItems.push({name:'引荐'})
            
          }
          if(zhiqian != null && zhiqian != '' && zhiqian != '0'){
            radioItems.push({name:'直签'})
            
          }
          radioItems[0].checked=true
          self.setData({
            radioItems:radioItems,
          })
        }
      }
    })
  },
  submitBussiness() {
    for (let i = 0; i < this.data.radioItems.length; i++) {
      if (this.data.radioItems[i].checked == true){
        this.setData({
          returnType: this.data.radioItems[i].name
        })
      }
    }
    if(this.data.customName == ''){
      wx.showToast({ title: "请输入客户全称！", icon: "none", duration: 3000 });
      return;
    }
    if(this.data.customPeople == ''){
      wx.showToast({ title: "请输入联系人姓名！", icon: "none", duration: 3000 });
      return;
    }
    if (this.data.productId == '') {
      wx.showToast({ title: "数据错误，请稍后重试！", icon: "none", duration: 3000 });
      return;
    }
    var phone = this.data.customPhone
    if(phone != ''){
      if (!(/^1[3456789]\d{9}$/.test(phone))) {
        wx.showToast({
          title: '手机格式错误',
          icon: 'none',
          duration: 3000
        })
        return;
      } 
    }

    if (this.data.valideCode == '') {
      wx.showToast({
        title: '请输入验证码',
        icon: 'none',
        duration: 3000
      })
      return;
    }

    let self = this
    app.request({
      url: 'api/wechat/bussiness',
      method: "GET",
      data: {
        productId: self.data.productId,
        returnType: self.data.returnType,
        customName: self.data.customName,
        customPeople:self.data.customPeople,
        customPhone:self.data.customPhone,
        customDescrption:self.data.customDescrption,
        code: self.data.valideCode
      },
      success: function (res) {
        console.log(res)
        if (res.data.result) {
          wx.switchTab({
            url: '/page/BusinessCenter/index',
          })
        }else{
          wx.showToast({ title: res.data.errMsg, icon: "none", duration: 3000 });
        }
      }
    })

  }
})